import { createRouter, createWebHashHistory } from "vue-router";
import { showToast } from 'vant';

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            component: () => import('../views/home/index')
        },
        {
            path: '/home',
            component: () => import('../views/home/index')
        },
        {
            path: '/cart',
            component: () => import('../views/cart'),
            meta:{
                isAuth:true
            }
        },
        {
            path: '/mine',
            component: () => import('../views/mine')
        },
        {
            path: '/order',
            component: () => import('../views/order'),
            meta:{
                isAuth:true
            }
        },
        {
            path: '/store',
            component: () => import('../views/store')
        },
        {
            path: '/createOrder',
            component: () => import('../views/createOrder'),
            meta:{
                isAuth:true
            }
        },
        {
            path: '/address',
            component: () => import('../views/address')
        },
        {
            path: '/addressEdit',
            component: () => import('../views/addressEdit'),
            meta:{
                isAuth:true
            }
        },
        {
            path: '/userInfo',
            component: () => import('../views/userInfo'),
            meta:{
                isAuth:true
            }
        },
        {
            path: '/login',
            component: () => import('../views/login')
        },
        {
            path: '/register',
            component: () => import('../views/register')
        }
    ]
})
router.beforeEach((to, from, next) => {
if(to.meta.isAuth){
    if(localStorage.isLogin === 'login'){
        next()
    }else{
        next('/login')
        showToast("请先登录!")
    }
}else{
    next()
}
})

export default router